<?php
namespace app\index\controller;

use app\index\model\JoinModel;
use app\index\model\UtilModel;
use think\Controller;
use think\Request;
use think\Loader;

class Index extends Controller
{
    /**
     * 员工登陆1
     * @return mixed
     */
    public function userLogin(){
        $joinModel = new JoinModel();
        if(Request::instance()->isPost()){
            $info['province_id'] = $data['province_id'] = input('province_id',0,'intval');
            $info['city_id'] = $data['city_id'] = input('city_id',0,'intval');
            $info['hospital_id'] = $data['hospital_id'] = input('hospital_id',0,'intval');
            $info['subject_id'] = $data['subject_id'] = input('subject_id',0,'intval');
            $info['count']= $data['count'] = input('count',0,'intval');
            $info['name'] = $check['name'] = input('user_name','','trim');
            $info['email'] = $check['email'] = input('user_email','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('user')->check($info)) {
                return $this->error($validate->getError());
            }

            //校验身份
            $check = $joinModel->checkUser($check);
            if($check){
                $data['join_type'] = 1;
                $data['add_time'] = time();
                $data['user_id'] = $check['user_id'];
                $res = $joinModel->joinMeetingInfo($data);
                if($res){
                    return $this->success('登陆成功','http://stream.gensee.com/webcast/site/entry/join-ea659b514bf741b5a1108c6b0761c6f5?nickName='.$check['user_name']);
//                    return $this->success('登陆成功','http://www.bizconfstreaming.cn/webcast/wljd.html?nickName='.$check['user_name']);
                }else{
                    return $this->error('登陆失败');
                }
            }else{
                return $this->error('身份校验失败');
            }

        }else{
            //判断是否符合身份
            $data['name'] = $user_name =  input('name','','trim');
            $data['email'] = $email = input('uid','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('check')->check($data)) {
                return $this->error($validate->getError());
            }
            $res = $joinModel->checkUser($data);
            if($res){
                $provinces = get_province();
                $subjects = get_subject();
                $this->assign('provinces',$provinces);
                $this->assign('subjects',$subjects);
            }
            $this->assign('check_num',$res);
            return $this->fetch('index/user_login');
        }
    }

    /**
     * 员工登陆3
     * @return mixed
     */
    public function userMeetingLogin(){
        $joinModel = new JoinModel();
        if(Request::instance()->isPost()){
            $info['province_id'] = $data['province_id'] = input('province_id',0,'intval');
            $info['city_id'] = $data['city_id'] = input('city_id',0,'intval');
            $info['hospital_id'] = $data['hospital_id'] = input('hospital_id',0,'intval');
            $info['subject_id'] = $data['subject_id'] = input('subject_id',0,'intval');
            $info['count']= $data['count'] = input('count',0,'intval');
            $info['name'] = $check['name'] = input('user_name','','trim');
            $info['email'] = $check['email'] = input('user_email','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('user')->check($info)) {
                return $this->error($validate->getError());
            }

            //校验身份
            $check = $joinModel->checkUser($check);
            if($check){
                $data['join_type'] = 3;
                $data['add_time'] = time();
                $data['user_id'] = $check['user_id'];
                $res = $joinModel->joinMeetingInfo($data);
                if($res){
                    $params = [
                        'id'     => '156071',
                        'userid' => $data['user_id'],
                        'name'   => $info['name'],
                        'avatar' => '',
                        'key'    => md5($data['user_id'].'408L55zut99I')
                    ];
                    header('Location:http://webcasting.bizconfstreaming.com/activity.php?a=userAssign&'.http_build_query($params));
                    exit;
                }else{
                    return $this->error('身份校验失败');
                }
            }else{
                return $this->error('身份校验失败');
            }

        }else{
            //判断是否符合身份
            $data['name'] = $user_name =  input('name','','trim');
            $data['email'] = $email = input('uid','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('check')->check($data)) {
                return $this->error($validate->getError());
            }
            $res = $joinModel->checkUser($data);
            if($res){
                $provinces = get_province();
                $subjects = get_subject();
                $this->assign('provinces',$provinces);
                $this->assign('subjects',$subjects);
            }
            $this->assign('check_num',$res);
            return $this->fetch('index/user_meeting_login');
        }
    }

    /**
     * 员工登陆4
     * @return mixed
     */
    public function userBackLogin(){
        $joinModel = new JoinModel();
        if(Request::instance()->isPost()){
            $info['province_id'] = $data['province_id'] = input('province_id',0,'intval');
            $info['city_id'] = $data['city_id'] = input('city_id',0,'intval');
            $info['hospital_id'] = $data['hospital_id'] = input('hospital_id',0,'intval');
            $info['subject_id'] = $data['subject_id'] = input('subject_id',0,'intval');
            $info['count']= $data['count'] = input('count',0,'intval');
            $info['name'] = $check['name'] = input('user_name','','trim');
            $info['email'] = $check['email'] = input('user_email','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('user')->check($info)) {
                return $this->error($validate->getError());
            }

            //校验身份
            $check = $joinModel->checkUser($check);
            if($check){
                $data['join_type'] = 4;
                $data['add_time'] = time();
                $data['user_id'] = $check['user_id'];
                $res = $joinModel->joinMeetingInfo($data);
                if($res){
                    $params = [
                        'id'     => '167959',
                        'userid' => $data['user_id'],
                        'name'   => $info['name'],
                        'avatar' => '',
                        'key'    => md5($data['user_id'].'G9rRC1nO9EB')
                    ];
                    header('Location:http://webcasting.bizconfstreaming.com/activity.php?a=userAssign&'.http_build_query($params));
                    exit;
                }else{
                    return $this->error('身份校验失败');
                }
            }else{
                return $this->error('身份校验失败');
            }

        }else{
            //判断是否符合身份
            $data['name'] = $user_name =  input('name','','trim');
            $data['email'] = $email = input('uid','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('check')->check($data)) {
                return $this->error($validate->getError());
            }
            $res = $joinModel->checkUser($data);
            if($res){
                $provinces = get_province();
                $subjects = get_subject();
                $this->assign('provinces',$provinces);
                $this->assign('subjects',$subjects);
            }
            $this->assign('check_num',$res);
            return $this->fetch('index/user_back_login');
        }
    }

    /**
     * 员工登陆5
     * @return mixed
     */
    public function userLogin1(){
        $joinModel = new JoinModel();
        if(Request::instance()->isPost()){
            $info['province_id'] = $data['province_id'] = input('province_id',0,'intval');
            $info['city_id'] = $data['city_id'] = input('city_id',0,'intval');
            $info['hospital_id'] = $data['hospital_id'] = input('hospital_id',0,'intval');
            $info['subject_id'] = $data['subject_id'] = input('subject_id',0,'intval');
            $info['count']= $data['count'] = input('count',0,'intval');
            $info['name'] = $check['name'] = input('user_name','','trim');
            $info['email'] = $check['email'] = input('user_email','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('user')->check($info)) {
                return $this->error($validate->getError());
            }

            //校验身份
            $check = $joinModel->checkUser($check);
            if($check){
                $data['join_type'] = 5;
                $data['add_time'] = time();
                $data['user_id'] = $check['user_id'];
                $res = $joinModel->joinMeetingInfo($data);
                if($res){
                    $params = [
                        'id'     => '181310',
                        'userid' => $data['user_id'],
                        'name'   => $info['name'],
                        'avatar' => '',
                        'key'    => md5($data['user_id'].'AMN5PlyIExDG')
                    ];
                    header('Location:http://webcasting.bizconfstreaming.com/activity.php?a=userAssign&'.http_build_query($params));
                    exit;
                }else{
                    return $this->error('身份校验失败');
                }
            }else{
                return $this->error('身份校验失败');
            }

        }else{
            //判断是否符合身份
            $data['name'] = $user_name =  input('name','','trim');
            $data['email'] = $email = input('uid','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('check')->check($data)) {
                return $this->error($validate->getError());
            }
            $res = $joinModel->checkUser($data);
            if($res){
                $provinces = get_province();
                $subjects = get_subject();
                $this->assign('provinces',$provinces);
                $this->assign('subjects',$subjects);
            }
            $this->assign('check_num',$res);
            return $this->fetch('index/user_login_1');
        }
    }
    /**
     * 员工登陆6
     * @return mixed
     */
    public function userLogin2(){
        $joinModel = new JoinModel();
        if(Request::instance()->isPost()){
            $info['province_id'] = $data['province_id'] = input('province_id',0,'intval');
            $info['city_id'] = $data['city_id'] = input('city_id',0,'intval');
            $info['hospital_id'] = $data['hospital_id'] = input('hospital_id',0,'intval');
            $info['subject_id'] = $data['subject_id'] = input('subject_id',0,'intval');
            $info['count']= $data['count'] = input('count',0,'intval');
            $info['name'] = $check['name'] = input('user_name','','trim');
            $info['email'] = $check['email'] = input('user_email','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('user')->check($info)) {
                return $this->error($validate->getError());
            }

            //校验身份
            $check = $joinModel->checkUser($check);
            if($check){
                $data['join_type'] = 6;
                $data['add_time'] = time();
                $data['user_id'] = $check['user_id'];
                $res = $joinModel->joinMeetingInfo($data);
                if($res){
                    $params = [
                        'id'     => '181311',
                        'userid' => $data['user_id'],
                        'name'   => $info['name'],
                        'avatar' => '',
                        'key'    => md5($data['user_id'].'oIDMuSW05XJh')
                    ];
                    header('Location:http://webcasting.bizconfstreaming.com/activity.php?a=userAssign&'.http_build_query($params));
                    exit;
                }else{
                    return $this->error('身份校验失败');
                }
            }else{
                return $this->error('身份校验失败');
            }

        }else{
            //判断是否符合身份
            $data['name'] = $user_name =  input('name','','trim');
            $data['email'] = $email = input('uid','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('check')->check($data)) {
                return $this->error($validate->getError());
            }
            $res = $joinModel->checkUser($data);
            if($res){
                $provinces = get_province();
                $subjects = get_subject();
                $this->assign('provinces',$provinces);
                $this->assign('subjects',$subjects);
            }
            $this->assign('check_num',$res);
            return $this->fetch('index/user_login_2');
        }
    }
    /**
     * 获取城市
     * @return mixed
     */
    public function getCity(){
        $province_id = input('province_id',0,'intval');
        return get_region($province_id);
    }
    /**
     * 获取医院
     */
    public function getHospital(){
       $city_id = input('city_id',0,'intval');
       return get_hospital($city_id);
    }

    /**
     * 导入医院数据
     * @return mixed|void
     */
    public function batchHospital(){
        if(Request::instance()->isPost()){
            $file_path = \think\Loader::action('Upload/batchHospital');
            if (empty($file_path)) {
                return $this->error('请选择上传文件');
            }
            $utilModel = new UtilModel();
            $hospitalData = $utilModel->importHospital($file_path);
            if($hospitalData == -1){
                return  $this->error('请选择xls和xlsx格式的文件');
            }elseif($hospitalData==-2){
                return  $this->error('没要要导入的数据');
            }elseif($hospitalData){
                return $this->success('导入成功');
            }else{
                return $this->error('导入失败');
            }
        }else{
            return $this->fetch();
        }
    }

    /**
     * 导入员工数据
     * @return mixed|void
     * @throws \app\index\model\PHPExcel_Exception
     * @throws \app\index\model\PHPExcel_Reader_Exception
     */
    public function batchUser(){
        if(Request::instance()->isPost()){
            $file_path = \think\Loader::action('Upload/batchUser');
            if (empty($file_path)) {
                return $this->error('请选择上传文件');
            }
            $utilModel = new UtilModel();
            $hospitalData = $utilModel->importUser($file_path);
            if($hospitalData == -1){
                return  $this->error('请选择xls和xlsx格式的文件');
            }elseif($hospitalData==-2){
                return  $this->error('没要要导入的数据');
            }elseif($hospitalData){
                return $this->success('导入成功');
            }else{
                return $this->error('导入失败');
            }
        }else{
            return $this->fetch();
        }
    }
}
